var salt = sessionStorage.getItem('usersalt');
layui.config({
	base: '../../../js/',
})

var app = new Vue({
  el: ".score",
  data: {
    multiSelect: [],
    radio:[],
    tfng:[],
    id:'',
    sortType:''
  },
  updated: function () {

  }
})

layui.use(['form', 'layer', 'laydate', 'table', 'multiSelect'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		formSelects = layui.formSelects,
		table = layui.table;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}
    $.post(url + "/baseUsers/getInfo",{Token:salt},function (data) {
        var data = data.data;
        if(data.roleId == 1){
          $(".subject_btn,.addNews_btn").show()
        }
    })
	/*时间选择器*/
	laydate.render({
		elem: '#statdate',
		trigger: 'click'
	});

	laydate.render({
		elem: '#enddate',
		trigger: 'click'
	});

	/*筛选*/
	$('#seleCheck,#findscri').on('click', function() {
        getDangerList()
	});

	/*搜索*/
	$('#findscri').on('click', function() {
		var inptext = $('#inptext').val();
		var box = /\s/g;
		var param = inptext.replace(box, '');
	});
	var company;
	/*取出所有的单位*/
	function getAllDanwei() {
		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {},
			success: function(data) {
				$('#danwei').empty();
				if(data.code == 0) {
					var list = data.data;
					company = list;
				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	getAllDanwei();

  /*------------tree.start------------*/
  var setting = {
    check: {
      enable: true
    },
    data: {
      simpleData: {
        enable: true
      }
    },
    callback: {
      onCheck: zTreeOnCheck
    }
  };
  var zNodes = company;
  $.fn.zTree.init($("#danwei"), setting, zNodes);

  function getSydwVal() {
    var treeArr = [];
    var treeObj = $.fn.zTree.getZTreeObj("danwei");
    var nodes = treeObj.getCheckedNodes(true);
    for(var i = 0; i < nodes.length; i++) {
      treeArr.push(nodes[i].id); //获取选中节点的值
    }
    return treeArr;
  }
  $('#resclas').click(function() {
    $("body").trigger("click");
    $('#danwei').show();


  });

  function zTreeOnCheck(event, treeId, treeNode) {
    var nval = "";
    var treeObj = $.fn.zTree.getZTreeObj("danwei");
    var nodes = treeObj.getCheckedNodes(true);
    for(var i = 0; i < nodes.length; i++) {
      nval += nodes[i].name + ",";
    }
    $('#resclas').val(nval.substr(0, nval.length - 1));
  };
	/*------------tree.end------------*/

	var tableIns;
  function getDangerList(searchText) {
    var sydwArr = getSydwVal();
    if(!searchText) {
      searchText = null;
    }

    var types = [];
    var obj_type = document.getElementById("type");
    for(var i = 0; i < obj_type.options.length; i++) {
      if(obj_type.options[i].selected) {
        types.push(obj_type.options[i].value); // 收集选中项
      }
    }

    tableIns = table.render({
      elem: '#dangerList',
      method: 'post',
      url: url + 'questionUser/getQuestionUser',
      where: {
        Token: salt, //用户码
        examType:$("#type").val(),
        title:$("#inptext").val(),
        entId:sydwArr.toString(),
        startTime:$("#statdate").val(),
        endTime:$("#enddate").val(),
        questionStatus:1
      },
      cellMinWidth: 95,
      page: true,
      height: "full-125",
      id: "dangerListTable",
      cols: [
        [{
          type: "numbers",
          width: 50,
          title: "序号",
        },
          {
            type: "checkbox",
            width: 50
          },
          {
            field: 'paperNaem',
            title: '试卷名称',
            align: 'center',
            templet: function(p) {
              return "<div class='paperName paperinfo' id='"+p.id +"' userName='"+p.userName+"' paperId='"+p.paperId+"' userId='"+p.userId+"' questionStatus='"+p.questionStatus+"'>"+p.paperNaem+"</div>";
            }
          },
          {
            field: '',
            title: '总题数',
            align: 'center',
            templet: function(p) {
              return "<div class='paperName'>" + p.questionNum + "</div>"
            }
          },
          {
            field: 'seat',
            title: '答题成绩',
            align: 'center',
            templet: function(p) {
              if(p.total){
                return "<div class='paperName'>" + p.total + "</div>";
              }else {

              }return "<div  class='paperName'></div>";

            }
          },
          {
            field: 'userName',
            title: '姓名',
            align: 'center',
            templet: function(p) {
                return "<div class='paperName'>" + p.userName + "</div>";
            }
          },
          {
            field: 'examType',
            title: '考试类型',
            align: 'center',
            templet: function(p) {
              if(p.examType == 0){
                return "<div class='paperName'>" + "模拟练习</div>";
              }else if (p.examType == 1){
                return "<div class='paperName'>" + "班组测试</div>";
              }else if (p.examType == 2){
                return "<div class='paperName'>" + "专项测试</div>";
              }
            }

          },
          {
            field: 'crtTime',
            title: '答题时间',
            align: 'center',
            templet: function(p) {
                return "<div class='paperName'>" + initDate(p.quizTime) + "</div>";
            }
          },
        ]
      ],
      done: function(res, curr, count) {

      }
    });

  }
  getDangerList();

	//新增
	$(".addNews_btn").click(function() {
		$(".model").show();
	});


	//题库
	$(".subject_btn").click(function() {
		location.href = "../subject/subject.html?nobtn=true"

	});
var deleteIdArr = new Array();
	//导出
	$(".export_btn").click(function() {
		var checkStatus = table.checkStatus('dangerListTable'),
		data = checkStatus.data;
		for(var i = 0; i < data.length; i++) {
			deleteIdArr.push(data[i].id);
		}
		if(data.length > 0) {
			
			$.post(url + 'questionUser/exportInfo', {
				Token: salt,
				ids: deleteIdArr.toString()
			}, function(result) {
				window.open(url + "file" + result.data);
			});
		} else {
			layer.msg("请选择需要导出的数据");
		}
	});
})



function showDetail(id) {

}
$("body").on("click",".paperName",function () {
  var $paperinfo = $(this).parents("tr").find(".paperinfo");
  var paperid = $paperinfo.attr("paperid");
  var username = $paperinfo.attr("username");
  var userId = $paperinfo.attr("userId");
  var questionStatus = $paperinfo.attr("questionStatus");
  var id = $paperinfo.attr("id");
  $.post(url+"questionUser/getInfo",{id:id,Token:salt},function (data) {
    if(data.code == 0){
      var data = data.data;
      app.radio = JSON.parse(data.radio);
      app.multiSelect = JSON.parse(data.multiSelect);
      app.tfng = JSON.parse(data.tfng);
      app.id = data.id;
      app.paperid = paperid;
      var sortType = data.sortType.split("—");
      app.sortType = sortType;
      $(".score").show();
    }
  })
})

$("body").on("click",".score ul li",function () {
  var id = $(this).attr("id");
  var questionId = $(this).attr("questionId");
  var paperid = $(this).attr("paperid");
  location.href="../score/score.html?id="+id+"&questionId="+questionId+"&paperid="+paperid;
})
$("body").on("click",".score .title .layui-tab-close",function () {
  $(".score").hide();
})

function initDate(str) {
  var str = new Date(str);
  return str.getFullYear() + "-" + (str.getMonth() + 1) + "-" + str.getDate()+ " " + str.getHours() + ":" + str.getMinutes();
};



















